package cn.edu.scau.cmi.zhangweiming.composite.consistent;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;

import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;

import cn.edu.scau.cmi.zhangweiming.composite.consistent.Organization;
import cn.edu.scau.cmi.zhangweiming.composite.consistent.Person;
import cn.edu.scau.cmi.zhangweiming.composite.consistent.Team;

import javafx.event.ActionEvent;

public class Controller implements Initializable{
	private Team root=new Team("root");
	private Map<String, Organization> map = new HashMap<>();
	private String parentName = "";
    private String nodeName = "";
    
    {
    	this.map.put(root.getname(),root);
    }
    
	@FXML
	private Button addteam,addperson,delect;
	@FXML
	private TextField parenttext,nodetext;
	@FXML
	private TextArea output;
	
	
	public void addTeam() {
        if (checkInput()) {
        	Team parent = this.getParent(parentName);
            if (parent == null) {
                return;
            }
            if (map.containsKey(nodeName)) {
                this.output.appendText("已存在的名字\n");
                return;
            }
            Team node = new Team(nodeName);
            parent.addChild(node);
            this.map.put(node.getname(), node);
            this.output.clear();
            this.display(root, 0);
        }
    }
	
	public void addPerson() {
        if (checkInput()) {
        	Team parent = this.getParent(parentName);
            if (parent == null) {
                return;
            }
            if (map.containsKey(nodeName)) {
                this.output.appendText("已存在的名字\n");
                return;
            }
            Organization node = new Person(nodeName);
            parent.addChild(node);
            this.map.put(node.getname(), node);
            this.output.clear();
            this.display(root, 0);
        }
    }
	
	public void delete() {
        if (checkInput()) {
        	Team parent = getParent(parentName);
            if (parent == null) {
                return;
            }
            Organization node = map.getOrDefault(nodeName, null);
            if (node == null) {
                this.output.appendText("不存在该节点！\n");
                return;
            }
            if (node == root) {
                this.output.appendText("该节点不可删除 \n");
                return;
            }
            parent.deleteChild(node);
            this.map.remove(nodeName);
            this.output.clear();
            this.display(root, 0);
        }
    }	
	
	private boolean checkInput() {
        parentName = parenttext.getText();
        nodeName = nodetext.getText();
        if ("".equals(this.parentName) || "".equals(this.nodeName)) {
            output.appendText("请完整输入父节点及操作节点！" + "\n");
            return false;
        }
        return true;
    }
	
	private Team getParent(String name) {
		Organization parent = map.getOrDefault(name, null);
        if (parent instanceof Person || parent == null) {
            this.output.appendText("Person不能再添加子节点。\n");
            return null;
        }
        return (Team) parent;
    }
	
	private void display(Organization root, int level) {
        if (level == 0) {
            this.output.appendText(root.getname()+ "\n");
        } else {
            for (int i = 0; i < level; i++) {
                this.output.appendText("\t");
            }
            this.output.appendText("	" + root.info() + "\n");
        }
        if (root instanceof Team) {
            for (Organization child : ((Team)root).getChildren()) {
                this.display(child, level + 1);
            }
        }
    }
	
	public void initialize(URL location, ResourceBundle resources) {
        this.display(root, 0);
    }
}
